﻿using AutoMapper;
using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys.Farmings;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys.Farmings
{
    /// <summary>
    /// 新增养殖保健信息命令处理
    /// </summary>
    public class AddFarmingHealthCommandHandlter : IRequestHandler<AddFarmingHealthCommand, ApiResult<int>>
    {
        /// <summary>
        /// 保健信息仓储
        /// </summary>
        private readonly IBaseRepository<FarmingHealthcareModel> farmingHealthcareRep;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="farmingHealthcareRep">保健信息仓储</param>
        /// <param name="mapper">映射</param>
        public AddFarmingHealthCommandHandlter(IBaseRepository<FarmingHealthcareModel> farmingHealthcareRep,IMapper mapper)
        {
            this.farmingHealthcareRep = farmingHealthcareRep;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(AddFarmingHealthCommand request, CancellationToken cancellationToken)
        {


            //新增保健信息
            var healthy=mapper.Map<FarmingHealthcareModel>(request);
            await  farmingHealthcareRep.AddAsync(healthy);

            return new ApiResult<int>
            {
                code = ResultEnumCode.Ok,
                data = 1,
                message = "添加保健信息成功"
            };

        }
    }
}
